{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5c1357c1",
   "metadata": {},
   "source": [
    "## 1.优化百度asr接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ee0792de",
   "metadata": {},
   "outputs": [],
   "source": [
    "from bdasr import fetch_token,asr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "2e31470b",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_KEY = 'V5iNAiKgmARc0RoGitFoXkh8'\n",
    "SECRET_KEY = '1ue3uc0oSXLAhRjYMgKSjXm1S6Gc8TwR'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "2bda641a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.e094e8f099e131f48fe477b9b0e80c70.2592000.1655295554.282335-26192305'"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hai_token = fetch_token(API_KEY,SECRET_KEY)\n",
    "hai_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "528351eb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"corpus_no\":\"7098307719959822178\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"北京科技馆。\"],\"sn\":\"16285943631652703555\"}\\n'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "asr(token=hai_token,AUDIO_FILE='./yuying/16k.pcm')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "82964e4b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pipwin in e:\\anaconda3\\lib\\site-packages (0.5.2)\n",
      "Requirement already satisfied: docopt in e:\\anaconda3\\lib\\site-packages (from pipwin) (0.6.2)\n",
      "Requirement already satisfied: six in e:\\anaconda3\\lib\\site-packages (from pipwin) (1.15.0)\n",
      "Requirement already satisfied: requests in e:\\anaconda3\\lib\\site-packages (from pipwin) (2.25.1)\n",
      "Requirement already satisfied: pyprind in e:\\anaconda3\\lib\\site-packages (from pipwin) (2.11.3)\n",
      "Requirement already satisfied: beautifulsoup4>=4.9.0 in e:\\anaconda3\\lib\\site-packages (from pipwin) (4.9.3)\n",
      "Requirement already satisfied: pySmartDL>=1.3.1 in e:\\anaconda3\\lib\\site-packages (from pipwin) (1.3.4)\n",
      "Requirement already satisfied: packaging in e:\\anaconda3\\lib\\site-packages (from pipwin) (20.9)\n",
      "Requirement already satisfied: js2py in e:\\anaconda3\\lib\\site-packages (from pipwin) (0.71)\n",
      "Requirement already satisfied: soupsieve>1.2 in e:\\anaconda3\\lib\\site-packages (from beautifulsoup4>=4.9.0->pipwin) (2.2.1)\n",
      "Requirement already satisfied: tzlocal>=1.2 in e:\\anaconda3\\lib\\site-packages (from js2py->pipwin) (4.2)\n",
      "Requirement already satisfied: pyjsparser>=2.5.1 in e:\\anaconda3\\lib\\site-packages (from js2py->pipwin) (2.7.1)\n",
      "Requirement already satisfied: tzdata in e:\\anaconda3\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (2022.1)\n",
      "Requirement already satisfied: backports.zoneinfo in e:\\anaconda3\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (0.2.1)\n",
      "Requirement already satisfied: pytz-deprecation-shim in e:\\anaconda3\\lib\\site-packages (from tzlocal>=1.2->js2py->pipwin) (0.1.0.post0)\n",
      "Requirement already satisfied: pyparsing>=2.0.2 in e:\\anaconda3\\lib\\site-packages (from packaging->pipwin) (2.4.7)\n",
      "Requirement already satisfied: idna<3,>=2.5 in e:\\anaconda3\\lib\\site-packages (from requests->pipwin) (2.10)\n",
      "Requirement already satisfied: certifi>=2017.4.17 in e:\\anaconda3\\lib\\site-packages (from requests->pipwin) (2020.12.5)\n",
      "Requirement already satisfied: chardet<5,>=3.0.2 in e:\\anaconda3\\lib\\site-packages (from requests->pipwin) (4.0.0)\n",
      "Requirement already satisfied: urllib3<1.27,>=1.21.1 in e:\\anaconda3\\lib\\site-packages (from requests->pipwin) (1.26.4)\n"
     ]
    }
   ],
   "source": [
    "!pip install pipwin"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ea710df4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Package `pyaudio` found in cache\n",
      "Downloading package . . .\n",
      "https://download.lfd.uci.edu/pythonlibs/x6hvwk7i/PyAudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "PyAudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "[*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 0 bytes / 111 kB @ 0 bytes/s [------------------] [0.0%, 0s left]   [*] 8 kB / 111 kB @ 7 kB/s [#-----------------] [7.2%, 0s left]   [*] 8 kB / 111 kB @ 6 kB/s [#-----------------] [7.2%, 0s left]   [*] 24 kB / 111 kB @ 17 kB/s [###---------------] [21.6%, 0s left]  [*] 24 kB / 111 kB @ 16 kB/s [###---------------] [21.6%, 0s left]  [*] 24 kB / 111 kB @ 15 kB/s [###---------------] [21.6%, 0s left]  [*] 40 kB / 111 kB @ 24 kB/s [######------------] [36.0%, 0s left]  [*] 40 kB / 111 kB @ 22 kB/s [######------------] [36.0%, 0s left]  [*] 40 kB / 111 kB @ 21 kB/s [######------------] [36.0%, 0s left]  [*] 40 kB / 111 kB @ 20 kB/s [######------------] [36.0%, 0s left]  [*] 56 kB / 111 kB @ 27 kB/s [#########---------] [50.4%, 0s left]  [*] 56 kB / 111 kB @ 25 kB/s [#########---------] [50.4%, 0s left]  [*] 56 kB / 111 kB @ 24 kB/s [#########---------] [50.4%, 0s left]  [*] 56 kB / 111 kB @ 23 kB/s [#########---------] [50.4%, 0s left]  [*] 72 kB / 111 kB @ 29 kB/s [###########-------] [64.8%, 0s left]  [*] 72 kB / 111 kB @ 28 kB/s [###########-------] [64.8%, 0s left]  [*] 72 kB / 111 kB @ 27 kB/s [###########-------] [64.8%, 0s left]  [*] 88 kB / 111 kB @ 31 kB/s [##############----] [79.3%, 0s left]  [*] 88 kB / 111 kB @ 30 kB/s [##############----] [79.3%, 0s left]  [*] 88 kB / 111 kB @ 29 kB/s [##############----] [79.3%, 0s left]  [*] 88 kB / 111 kB @ 29 kB/s [##############----] [79.3%, 0s left]  Processing c:\\users\\86180\\pipwin\\pyaudio-0.2.11-cp38-cp38-win_amd64.whl\n",
      "PyAudio is already installed with the same version as the provided wheel. Use --force-reinstall to force an installation of the wheel.\n",
      "[*] 111 kB / 111 kB @ 29 kB/s [##################] [100%, 0s left]    \n"
     ]
    }
   ],
   "source": [
    "!pipwin install PyAudio"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "ef0239d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: SpeechRecognition in e:\\anaconda3\\lib\\site-packages (3.8.1)\n"
     ]
    }
   ],
   "source": [
    "!pip install SpeechRecognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "89847aa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source) \n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cfc5efe",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cd9fb7b7",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
